<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>ES入门 - 分片和副本 | java知识体系</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/lm_blog_vdoing/img/favicon.ico">
    <meta name="description" content="lm_blog">
    <meta name="author" content="小猫爱吃鱼">
    <meta name="keywords" content="小猫爱吃鱼|java开发学习">
    <link rel="preload" href="/lm_blog_vdoing/assets/css/0.styles.6837ef6d.css" as="style"><link rel="preload" href="/lm_blog_vdoing/assets/js/app.fa0e5fdd.js" as="script"><link rel="preload" href="/lm_blog_vdoing/assets/js/2.d9c93b35.js" as="script"><link rel="preload" href="/lm_blog_vdoing/assets/js/23.87e8eccb.js" as="script"><link rel="preload" href="/lm_blog_vdoing/assets/js/5.e0bc0396.js" as="script"><link rel="preload" href="/lm_blog_vdoing/assets/js/13.ee87d1ca.js" as="script"><link rel="preload" href="/lm_blog_vdoing/assets/js/14.9d4d9cf6.js" as="script"><link rel="preload" href="/lm_blog_vdoing/assets/js/9.fc3a629f.js" as="script"><link rel="preload" href="/lm_blog_vdoing/assets/js/10.4c34cde9.js" as="script"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/11.bccd7768.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/12.e841fec0.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/15.ea506303.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/16.2da770e9.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/17.2e0242dc.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/18.304c9e7d.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/19.4b93faf0.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/20.87daf9d0.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/21.058f688b.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/22.9937ce9d.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/24.1688627c.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/25.04906008.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/26.494f706b.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/27.01c810da.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/28.9fbe8e29.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/29.3a1e938a.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/3.80bae7d2.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/30.4adb3ea1.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/31.5001aed0.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/32.306c6420.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/33.5fcce29f.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/34.2de40b8f.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/4.d2df651a.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/6.cbc45c4c.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/7.fd589d8e.js"><link rel="prefetch" href="/lm_blog_vdoing/assets/js/8.35156f8e.js">
    <link rel="stylesheet" href="/lm_blog_vdoing/assets/css/0.styles.6837ef6d.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/lm_blog_vdoing/" class="home-link router-link-active"><!----> <span class="site-name">java知识体系</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/lm_blog_vdoing/" class="nav-link">首页​</a></div><div class="nav-item"><a href="/lm_blog_vdoing/pages/f6054a/" class="nav-link">❁导航​</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="计算机核心" class="dropdown-title"><!----> <span class="title" style="display:;">计算机核心</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/db72cf/" class="nav-link">数据结构</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/39e2a1/" class="nav-link">计算机网络</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Java" class="dropdown-title"><!----> <span class="title" style="display:;">Java</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>Java基础</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/e05ef5/" class="nav-link">JavaSE</a></li></ul></li><li class="dropdown-item"><h4>JavaWeb</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/5cda88/" class="nav-link">HTML</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/cea341/" class="nav-link">CSS</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/d893c0/" class="nav-link">JavaScript</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/8f83ba/" class="nav-link">Servlet</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/8bc1c4/" class="nav-link">MVC</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/f883e2/" class="nav-link">filter|listener</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Spring生态" class="dropdown-title"><!----> <span class="title" style="display:;">Spring生态</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/462a90/" class="nav-link">Spring5</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/2e990c/" class="nav-link">SpringMVC</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/0d4af0/" class="nav-link">SpringBoot2</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/870083/" class="nav-link">SpringCloud</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/66babb/" class="nav-link">SpringSecurity</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="中间件" class="dropdown-title"><!----> <span class="title" style="display:;">中间件</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>消息队列</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/e645d9/" class="nav-link">RabbitMQ</a></li></ul></li><li class="dropdown-item"><h4>服务器</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/9551ee/" class="nav-link">Nginx🌐</a></li></ul></li><li class="dropdown-item"><h4>服务框架</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/48771f/" class="nav-link">Dubbo</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="大数据" class="dropdown-title"><!----> <span class="title" style="display:;">大数据</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>Python</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/b9268d/" class="nav-link">Python基础</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/e0bd06/" class="nav-link">数据分析</a></li></ul></li><li class="dropdown-item"><h4>环境搭建</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/600247/" class="nav-link">Hadoop集群</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/cdeb68/" class="nav-link">KVM虚拟化技术</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="数据库" class="dropdown-title"><!----> <span class="title" style="display:;">数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>SQL 数据库</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/e72480/" class="nav-link">MySQL</a></li></ul></li><li class="dropdown-item"><h4>NoSQL 数据库</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/e9cc9f/" class="nav-link">Redis</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/91197c/" class="nav-link">MongoDB</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/7b46ed/" class="nav-link">ElasticSearch</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="工具" class="dropdown-title"><!----> <span class="title" style="display:;">工具</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>部署</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/99e9dc/" class="nav-link">Linux</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/90cc29/" class="nav-link">Docker</a></li></ul></li><li class="dropdown-item"><h4>管理</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/45eca1/" class="nav-link">Maven</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/34892c/" class="nav-link">Git</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="面试" class="dropdown-title"><!----> <span class="title" style="display:;">面试</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/eab19d/" class="nav-link">十大排序算法</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/40ee62/" class="nav-link">力扣算法题</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/39558d/" class="nav-link">初级开发篇</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/47c622/" class="nav-link">中高进阶篇</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><!----> <span class="title" style="display:;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/myfriends/" class="nav-link">友情链接</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="本站" class="dropdown-title"><!----> <span class="title" style="display:;">本站</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>索引</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/categories/" class="nav-link">分类</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/tags/" class="nav-link">标签</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/archives/" class="nav-link">归档</a></li></ul></li><li class="dropdown-item"><h4>其他</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/9013e4/" class="nav-link">关于</a></li></ul></li></ul></div></div> <a href="https://gitee.com/lmgghub/lm_blog" target="_blank" rel="noopener noreferrer" class="repo-link">
    查看源码
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://cdn.staticaly.com/gh/xustudyxu/image-hosting@master/20220627/mmexport1656324824543.124zxwkqyzlc.webp"> <div class="blogger-info"><h3>lm_blog</h3> <span>持续学习!</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/lm_blog_vdoing/" class="nav-link">首页​</a></div><div class="nav-item"><a href="/lm_blog_vdoing/pages/f6054a/" class="nav-link">❁导航​</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="计算机核心" class="dropdown-title"><!----> <span class="title" style="display:;">计算机核心</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/db72cf/" class="nav-link">数据结构</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/39e2a1/" class="nav-link">计算机网络</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Java" class="dropdown-title"><!----> <span class="title" style="display:;">Java</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>Java基础</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/e05ef5/" class="nav-link">JavaSE</a></li></ul></li><li class="dropdown-item"><h4>JavaWeb</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/5cda88/" class="nav-link">HTML</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/cea341/" class="nav-link">CSS</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/d893c0/" class="nav-link">JavaScript</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/8f83ba/" class="nav-link">Servlet</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/8bc1c4/" class="nav-link">MVC</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/f883e2/" class="nav-link">filter|listener</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Spring生态" class="dropdown-title"><!----> <span class="title" style="display:;">Spring生态</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/462a90/" class="nav-link">Spring5</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/2e990c/" class="nav-link">SpringMVC</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/0d4af0/" class="nav-link">SpringBoot2</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/870083/" class="nav-link">SpringCloud</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/66babb/" class="nav-link">SpringSecurity</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="中间件" class="dropdown-title"><!----> <span class="title" style="display:;">中间件</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>消息队列</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/e645d9/" class="nav-link">RabbitMQ</a></li></ul></li><li class="dropdown-item"><h4>服务器</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/9551ee/" class="nav-link">Nginx🌐</a></li></ul></li><li class="dropdown-item"><h4>服务框架</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/48771f/" class="nav-link">Dubbo</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="大数据" class="dropdown-title"><!----> <span class="title" style="display:;">大数据</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>Python</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/b9268d/" class="nav-link">Python基础</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/e0bd06/" class="nav-link">数据分析</a></li></ul></li><li class="dropdown-item"><h4>环境搭建</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/600247/" class="nav-link">Hadoop集群</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/cdeb68/" class="nav-link">KVM虚拟化技术</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="数据库" class="dropdown-title"><!----> <span class="title" style="display:;">数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>SQL 数据库</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/e72480/" class="nav-link">MySQL</a></li></ul></li><li class="dropdown-item"><h4>NoSQL 数据库</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/e9cc9f/" class="nav-link">Redis</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/91197c/" class="nav-link">MongoDB</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/7b46ed/" class="nav-link">ElasticSearch</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="工具" class="dropdown-title"><!----> <span class="title" style="display:;">工具</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>部署</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/99e9dc/" class="nav-link">Linux</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/90cc29/" class="nav-link">Docker</a></li></ul></li><li class="dropdown-item"><h4>管理</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/45eca1/" class="nav-link">Maven</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/34892c/" class="nav-link">Git</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="面试" class="dropdown-title"><!----> <span class="title" style="display:;">面试</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/eab19d/" class="nav-link">十大排序算法</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/40ee62/" class="nav-link">力扣算法题</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/39558d/" class="nav-link">初级开发篇</a></li><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/47c622/" class="nav-link">中高进阶篇</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><!----> <span class="title" style="display:;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/lm_blog_vdoing/pages/myfriends/" class="nav-link">友情链接</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="本站" class="dropdown-title"><!----> <span class="title" style="display:;">本站</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>索引</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/categories/" class="nav-link">分类</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/tags/" class="nav-link">标签</a></li><li class="dropdown-subitem"><a href="/lm_blog_vdoing/archives/" class="nav-link">归档</a></li></ul></li><li class="dropdown-item"><h4>其他</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/lm_blog_vdoing/pages/9013e4/" class="nav-link">关于</a></li></ul></li></ul></div></div> <a href="https://gitee.com/lmgghub/lm_blog" target="_blank" rel="noopener noreferrer" class="repo-link">
    查看源码
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>MySQL - 基础</span> <!----></p> <!----></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>MySQL - 进阶</span> <!----></p> <!----></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>MySQL - 运维部署</span> <!----></p> <!----></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>NoSQL - Redis</span> <!----></p> <!----></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>NoSQL - MongoDB</span> <!----></p> <!----></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>NoSQL - ElasticSearch</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/lm_blog_vdoing/pages/7b46ed/" class="sidebar-link">ElasticeSarch的概述</a></li><li><a href="/lm_blog_vdoing/pages/596552/" class="sidebar-link">ES入门 - 环境安装</a></li><li><a href="/lm_blog_vdoing/pages/bc6018/" class="sidebar-link">ES入门 - 基本使用：索引和文档</a></li><li><a href="/lm_blog_vdoing/pages/6e6e0c/" aria-current="page" class="active sidebar-link">ES入门 - 分片和副本</a></li><li><a href="/lm_blog_vdoing/pages/3d97b1/" class="sidebar-link">ES入门 - 分析器</a></li><li><a href="/lm_blog_vdoing/pages/b54e3a/" class="sidebar-link">ES入门 - java整合</a></li><li><a href="/lm_blog_vdoing/pages/7e4dff/" class="sidebar-link">ES进阶 - 分片操作原理</a></li><li><a href="/lm_blog_vdoing/pages/b82771/" class="sidebar-link">ES进阶 - 并发问题</a></li><li><a href="/lm_blog_vdoing/pages/c3becc/" class="sidebar-link">ES进阶 - 集群搭建</a></li><li><a href="/lm_blog_vdoing/pages/6d9622/" class="sidebar-link">ES进阶 - 优化配置</a></li><li><a href="/lm_blog_vdoing/pages/da009f/" class="sidebar-link">ES进阶 - 框架整合</a></li><li><a href="/lm_blog_vdoing/pages/7cb809/" class="sidebar-link">ElasticSearch面试题</a></li></ul></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-1baff76c><div class="articleInfo" data-v-1baff76c><ul class="breadcrumbs" data-v-1baff76c><li data-v-1baff76c><a href="/lm_blog_vdoing/" title="首页" class="iconfont icon-home router-link-active" data-v-1baff76c></a></li> <li data-v-1baff76c><a href="/lm_blog_vdoing/categories/?category=notes" title="分类" data-v-1baff76c>notes</a></li><li data-v-1baff76c><a href="/lm_blog_vdoing/categories/?category=database" title="分类" data-v-1baff76c>database</a></li><li data-v-1baff76c><a href="/lm_blog_vdoing/categories/?category=elasticSearch" title="分类" data-v-1baff76c>elasticSearch</a></li></ul> <div class="info" data-v-1baff76c><div title="作者" class="author iconfont icon-touxiang" data-v-1baff76c><a href="javascript:;" data-v-1baff76c>lm</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-1baff76c><a href="javascript:;" data-v-1baff76c>2023-02-20</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">ES入门 - 分片和副本<!----></h1> <!----> <div class="theme-vdoing-content content__default"><h1 id="es入门-深入了解-分片和副本"><a href="#es入门-深入了解-分片和副本" class="header-anchor">#</a> ES入门 - 深入了解：分片和副本</h1> <h2 id="elasticsearch-分布式集群和路由计算"><a href="#elasticsearch-分布式集群和路由计算" class="header-anchor">#</a> ElasticSearch 分布式集群和路由计算</h2> <h3 id="分布式集群"><a href="#分布式集群" class="header-anchor">#</a> 分布式集群</h3> <p>我们在包含一个空节点的集群内创建名为 users 的索引，为了演示目的，我们将分配 3 个主分片和一份副本（每个主分片拥有一个副本分片）</p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;settings&quot;</span> <span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">&quot;number_of_shards&quot;</span> <span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>  <span class="token comment">// 3 个主分片</span>
        <span class="token property">&quot;number_of_replicas&quot;</span> <span class="token operator">:</span> <span class="token number">1</span>  <span class="token comment">// 每 1 个主分片都有 1 个副本</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h4 id="故障转移"><a href="#故障转移" class="header-anchor">#</a> 故障转移</h4> <p>​	当启动一个节点时，有可能导致单点故障问题。如果在启动一个跟第一个节点拥有同样的 cluster.name 的节点。该节点则会自动发现集群并加入。集群数据中所有的主分片和副本分片会重新分配</p> <h4 id="水平扩容"><a href="#水平扩容" class="header-anchor">#</a> 水平扩容</h4> <p>当我们启动第三个节点时，主分片和副本分片会重新分配如下</p> <p><img alt="es_067" data-src="https://cdn.staticaly.com/gh/LiuMWork/image-hosting@master/es_md/es_067.jpg" loading="lazy" class="lazy"></p> <p>如果我们需要重新设置分片数目，副本分片越多时，程序的吞吐量会越高。</p> <p>当然相同节点数目的集群上增加更多的副本分片并不能提高性能，因为每个分片从节点上获得的资源会变少。你需要增加更多的硬件资源来提升吞吐量。</p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;number_of_replicas&quot;</span> <span class="token operator">:</span> <span class="token number">2</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>在 <strong>Postman</strong> 发送 <code>PUT</code> 请求：http://127.0.0.1:7001/users/_settings</p> <p><img alt="es_068" data-src="https://cdn.staticaly.com/gh/LiuMWork/image-hosting@master/es_md/es_068.jpg" loading="lazy" class="lazy"></p> <h3 id="宕机故障"><a href="#宕机故障" class="header-anchor">#</a> 宕机故障</h3> <ul><li>主节点挂掉，会重新选举出新的主节点来保证正常的工作。</li> <li>重启挂掉的节点，用尝试复用之前的分片，同时仅从主分片复制发生了修改的数据文件。</li></ul> <h3 id="路由计算"><a href="#路由计算" class="header-anchor">#</a> 路由计算</h3> <p>​	当检索一个文档的时候，文档会被存储到一个主分片中。 ElasticSearch 如何知道一个文档应该存放到哪个分片中呢？当我们创建文档时，它如何决定这个文档应当被存储在分片 1 还是分片 2 中呢？首先这肯定不会是随机的，否则将来要获取文档的时候我们就不知道从何处寻找了。实际上，这个过程是根据下面这个公式决定的：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>shard <span class="token operator">=</span> <span class="token function">hash</span><span class="token punctuation">(</span>routing<span class="token punctuation">)</span> <span class="token operator">%</span> number_of_primary_shards
位置 <span class="token operator">=</span> <span class="token function">hash</span><span class="token punctuation">(</span>路由哈希值<span class="token punctuation">)</span> $ 分片总数
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>​	routing 是一个可变值，默认是文档的 _id ，也可以设置成一个自定义的值。routing 通过 hash 函数生成一个数字，然后这个数字再除以 number_of_primary_shards（主分片的数量）后得到余数。这个分布在 0 到 number_of_primary_shards-1 之间的余数，就是我们所寻求的文档所在分片的位置。</p> <p>​	这就解释了为什么我们要在创建索引的时候就确定好主分片的数量并且永远不会改变这个数量：因为如果数量变化了，那么所有之前路由的值都会无效，文档也再也找不到了。</p> <p>​	所有的文档 API（ get、index、delete、bulk、update 以及 mget ）都接受一个叫做 routing 的路由参数，通过这个参数我们可以自定义文档到分片的映射。一个自定义的路由参数可以用来确保所有相关的文档——例如所有属于同一个用户的文档——都被存储到同一个分片中。</p> <h2 id="elasticsearch-分片控制流程"><a href="#elasticsearch-分片控制流程" class="header-anchor">#</a> ElasticSearch 分片控制流程</h2> <h3 id="协调节点"><a href="#协调节点" class="header-anchor">#</a> 协调节点</h3> <p>​	我们可以发送请求到集群中的任一节点。每个节点都有能力处理任意请求。每个节点都知道集群中任一文档位置，所以可以直接将请求转发到需要的节点上。 将所有的请求发送到 Node 7001，我们将其称为 协调节点(coordinating node)。也可以理解为转发到其他节点的节点。</p> <blockquote><p>在发送请求时，可以采用轮训算法做负载</p></blockquote> <h3 id="写流程"><a href="#写流程" class="header-anchor">#</a> 写流程</h3> <p>新建、索引和删除请求都是<code>写</code>操作，必须在主分片上面完成写入操作之后才能被复制到相关的副本分片</p> <p><img alt="es_070" data-src="https://cdn.staticaly.com/gh/LiuMWork/image-hosting@master/es_md/es_070.webp" loading="lazy" class="lazy"></p> <p>写操作步骤顺序：</p> <ol><li>客户端向 Node 1 发送新建、索引或者删除请求</li> <li>节点使用文档的 <code>_id</code> 确定文档属于分片 <code>P0</code>。请求会被转发到 Node 3，因为分片 <code>P0</code> 的主分片目前被分配在 Node 3 上</li> <li>Node 3 在主分片上面执行请求。如果成功了，它将请求并行转发到 Node 1 和 Node 2 的副本分片上。一旦所有的副本分片都报告成功，Node 3 将向协调节点报告成功，协调节点向客户端报告成功</li></ol> <p>在客户端收到成功响应时，文档变更已经在主分片和所有副本分片执行完成，变更是安全的。有一些可选的请求参数允许您影响这个过程，可能以数据安全为代价提升性能。这些选项很少使用，因为 ElasticSearch 已经很快处理了，但是为了完整起见，请参考下面表格：</p> <table><thead><tr><th>参数</th> <th>含义</th></tr></thead> <tbody><tr><td>consistency</td> <td>consistency，即一致性。在默认设置下，即使仅仅是在试图执行一个_写_操作之前，主分片都会要求必须要有规定数量(quorum)（或者换种说法，也即必须要有大多数）的分片副本处于活跃可用状态，才会去执行<code>写</code>操作(其中分片副本 可以是主分片或者副本分片)。这是为了避免在发生网络分区故障（network partition）的时候进行<code>写</code>操作，进而导致数据不一致。<code>规定数量</code>即： <strong>int( (primary + number_of_replicas) / 2 ) + 1</strong> consistency 参数的值可以设为 <code>one</code> （只要主分片状态 ok 就允许执行<code>写</code>操 作）,<code>all</code>（必须要主分片和所有副本分片的状态没问题才允许执行_写_操作），或 quorum。默认值为 quorum， 即大多数的分片副本状态没问题就允许执行<code>写</code> 操作。 注意，规定数量的计算公式中 number_of_replicas 指的是在索引设置中的设定副本分片数，而不是指当前处理活动状态的副本分片数。如果你的索引设置中指定了当前索引拥有三个副本分片，那规定数量的计算结果即: <strong>int( (primary + 3 replicas) / 2 ) + 1 = 3</strong> 如果此时你只启动两个节点，那么处于活跃状态的分片副本数量就达不到规定数量，也因此您将无法检索和删除任何文档。</td></tr> <tr><td>timeout</td> <td>如果没有足够的副本分片会发生什么？ElasticSearch 会等待，希望更多的分片出现。默认情况下，它最多等待 1 分钟。如果你需要，你可以使用 timeout 参数使它更早终止：100 100 毫秒，30s 是 30 秒。</td></tr></tbody></table> <blockquote><p>新索引默认有 1 个副本分片，这意味着为满足规定数量应该需要两个活动的分片副本。但是，这些默认的设置会阻止我们在单一节点上做任何事情。为了避免这个问题，要求只有当 number_of_replicas 大于 1 的时候，规定数量才会执行。</p></blockquote> <h3 id="读流程"><a href="#读流程" class="header-anchor">#</a> 读流程</h3> <p>我们可以从主分片或者从其它任意副本分片检索文档</p> <p><img alt="es_071" data-src="https://cdn.staticaly.com/gh/LiuMWork/image-hosting@master/es_md/es_071.webp" loading="lazy" class="lazy"></p> <p>读流程顺序步骤：</p> <ol><li>客户端向 Node 1 发送获取请求</li> <li>节点使用文档的 <code>_id</code> 来确定文档属于分片 <code>P0</code>。分片 <code>P0</code> 的副本分片存在于所有的三个节点上。在这种情况下，它将请求转发到 Node 2</li> <li>Node 2 将文档返回给 Node 1，然后将文档返回给客户端</li></ol> <p><strong>每次都是 Node 2返回文档数据吗？</strong></p> <p>不是，采用轮询。</p> <p>在处理读取请求时，协调节点在每次请求的时候都会通过轮询所有的副本分片来达到负载均衡。在文档被检索时，已经被检索的文档可能已经存在于主分片上，但是还没有复制到副本分片。在这种情况下，副本分片可能会报告文档不存在，但是主分片可能成功返回文档。一旦索引请求成功返回给用户，文档在主分片和副本分片都是可用的。</p> <h3 id="更新流程"><a href="#更新流程" class="header-anchor">#</a> 更新流程</h3> <p>部分更新一个文档，结合了之前的读和写流程：</p> <p><img alt="es_072" data-src="https://cdn.staticaly.com/gh/LiuMWork/image-hosting@master/es_md/es_072.webp" loading="lazy" class="lazy"></p> <p>更新流程步骤顺序：</p> <ol><li>客户端向 Node 1 发送更新请求</li> <li>节点使用文档的 <code>_id</code> 来确定文档属于分片 <code>P0</code>，它将请求转发到主分片所在的 Node 3,因为分片 <code>P0</code> 的主分片目前被分配在 Node 3 上</li> <li>Node 3 从主分片检索文档，修改 <code>_source</code> 字段中的 JSON 数据，并且尝试重新检索主分片的文档。如果文档已经被另一个进程修改，它会重试步骤 3，超过 retry_on_conflict 次数后放弃</li> <li>如果 Node 3 成功地更新文档，它将新版本的文档并行转发到 Node 1 和 Node 2 上的副本分片，重新建立索引。一旦所有副本分片都返回成功，Node 3 向协调节点也返回成功，协调节点向客户端返回成功</li></ol> <p><strong>主分片同步到副本分片时，是转发更新请求吗？</strong></p> <p>不是。当主分片把更改转发到副本分片时，它不会转发更新请求。相反，它转发完整文档的新版本。请记住，这些数据更改文档将会异步转发到副本分片，并且不能保证数据更改文档以发送它们相同的顺序到达。如果 ElasticSearch 仅转发更改请求，则可能以错误的顺序应用更改，导致得到损坏的文档。</p> <h3 id="多文档操作流程"><a href="#多文档操作流程" class="header-anchor">#</a> 多文档操作流程</h3> <h3 id="mget流程"><a href="#mget流程" class="header-anchor">#</a> mget流程</h3> <p><code>mget</code> 和 bulk API 的模式类似于单文档模式。区别在于协调节点知道每个文档存在于哪个分片中。它将整个多文档请求分解成「每个分片」的多文档请求，并且将这些请求并行转发到每个参与节点。</p> <p><img alt="es_073" data-src="https://cdn.staticaly.com/gh/LiuMWork/image-hosting@master/es_md/es_073.webp" loading="lazy" class="lazy"></p> <p>用单个 <code>mget</code> 请求取回多个文档所需的步骤顺序:</p> <ol><li>客户端向 Node 1 发送 mget 请求</li> <li>Node 1 为每个分片构建多文档获取请求，然后并行转发这些请求到托管在每个所需的主分片或者副本分片的节点上。一旦收到所有答复，Node 1 构建响应并将其返回给客户端</li></ol> <p>可以对 docs 数组中每个文档设置 routing 参数</p> <h3 id="bulk-api流程"><a href="#bulk-api流程" class="header-anchor">#</a> bulk API流程</h3> <p><code>bulk API</code> 允许在单个批量请求中执行多个创建、索引、删除和更新请求</p> <p><img alt="es_074" data-src="https://cdn.staticaly.com/gh/LiuMWork/image-hosting@master/es_md/es_074.webp" loading="lazy" class="lazy"></p> <p><code>bulk API</code> 按执行步骤顺序：</p> <ol><li>客户端向 Node 1 发送 <code>bulk</code> 请求</li> <li>Node 1 为每个节点创建一个批量请求，并将这些请求并行转发到每个包含主分片的节点主机</li> <li>主分片一个接一个按顺序执行每个操作。当每个操作成功时，主分片并行转发新文档（或删除）到副本分片，然后执行下一个操作。一旦所有的副本分片报告所有操作成功，该节点将向协调节点报告成功，协调节点将这些响应收集整理并返回给客户端。</li></ol> <h2 id="参考文章"><a href="#参考文章" class="header-anchor">#</a> 参考文章</h2> <p>https://frxcat.fun/middleware/ElasticSearch/ElasticSearch_Slice_control_process/</p></div></div> <!----> <div class="page-edit"><!----> <div class="tags"><a href="/lm_blog_vdoing/tags/?tag=ElasticSearch" title="标签">#ElasticSearch</a></div> <!----></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/lm_blog_vdoing/pages/bc6018/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">ES入门 - 基本使用：索引和文档</div></a> <a href="/lm_blog_vdoing/pages/3d97b1/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">ES入门 - 分析器</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/lm_blog_vdoing/pages/bc6018/" class="prev">ES入门 - 基本使用：索引和文档</a></span> <span class="next"><a href="/lm_blog_vdoing/pages/3d97b1/">ES入门 - 分析器</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/lm_blog_vdoing/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/lm_blog_vdoing/pages/6d9622/"><div>
            ES进阶 - 优化配置
            <!----></div></a> <span class="date">02-20</span></dt></dl><dl><dd>02</dd> <dt><a href="/lm_blog_vdoing/pages/7e4dff/"><div>
            ES进阶 - 分片操作原理
            <!----></div></a> <span class="date">02-20</span></dt></dl><dl><dd>03</dd> <dt><a href="/lm_blog_vdoing/pages/b82771/"><div>
            ES进阶 - 并发问题
            <!----></div></a> <span class="date">02-20</span></dt></dl> <dl><dd></dd> <dt><a href="/lm_blog_vdoing/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:1512375133@qq.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://gitee.com/lmgghub/" title="gitee" target="_blank" class="iconfont icon-gitee"></a><a href="https://juejin.cn/user/2295436010326072" title="掘金" target="_blank" class="iconfont icon-juejin"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2023-2023
    <span>lm </span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div><div></div><div></div><div id="tcomment"></div><!----></div></div>
    <script src="/lm_blog_vdoing/assets/js/app.fa0e5fdd.js" defer></script><script src="/lm_blog_vdoing/assets/js/2.d9c93b35.js" defer></script><script src="/lm_blog_vdoing/assets/js/23.87e8eccb.js" defer></script><script src="/lm_blog_vdoing/assets/js/5.e0bc0396.js" defer></script><script src="/lm_blog_vdoing/assets/js/13.ee87d1ca.js" defer></script><script src="/lm_blog_vdoing/assets/js/14.9d4d9cf6.js" defer></script><script src="/lm_blog_vdoing/assets/js/9.fc3a629f.js" defer></script><script src="/lm_blog_vdoing/assets/js/10.4c34cde9.js" defer></script>
  </body>
</html>
